package com.youlu.campus.admin.domain;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.common.base.enums.CodeEnum;
import com.youlu.campus.domain.entity.PlatformDomainConfig;
import com.youlu.campus.domain.entity.PlatformDomainConfigPageQuery;
import com.youlu.campus.domain.service.PlatformDomainConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 域名配置控制器
 *
 * @Description: DomainConfigController
 * @Date 2025/10/21
 * @module DomainConfig
 * @author: su
 */
@Slf4j
@RestController
@RequestMapping("/admin/domain-config")
public class DomainConfigController {

    @Autowired
    private PlatformDomainConfigService domainConfigService;

    /**
     * 创建域名配置
     *
     * @param domainConfig 域名配置
     * @return 创建结果
     */
    @PostMapping("/save")
    public QResult<PlatformDomainConfig> createDomainConfig(@RequestBody PlatformDomainConfig domainConfig) {
        PlatformDomainConfig savedConfig = domainConfigService.save(domainConfig);
        return QResult.success(savedConfig);
    }

    /**
     * 根据id查询域名配置
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public QResult<PlatformDomainConfig> findById(@RequestParam String id) {
        PlatformDomainConfig savedConfig = domainConfigService.findById(id);
        return QResult.success(savedConfig);
    }


    /**
     * 根据域名查询配置
     *
     * @param domain 域名
     * @return 查询结果
     */
    @GetMapping("/domain")
    public QResult<PlatformDomainConfig> findByDomain(@RequestParam String domain) {
        PlatformDomainConfig domainConfig = domainConfigService.findByDomain(domain);
        if (domainConfig == null) {
            return QResult.fail(CodeEnum.NOT_FOUND.getCode(), "域名不存在");
        }
        return QResult.success(domainConfig);
    }


    /**
     * 分页查询域名配置
     *
     * @param query
     * @return
     */
    @PostMapping("/find-page")
    public QResult<Page<PlatformDomainConfig>> findByPage(@RequestBody PlatformDomainConfigPageQuery query) {
        Page<PlatformDomainConfig> configPage = domainConfigService.findByPage(query);
        return QResult.success(configPage);
    }


    /**
     * 删除域名配置
     *
     * @param query
     * @return 删除结果
     */
    @PostMapping("/delete")
    public QResult delete(@RequestBody PlatformDomainConfigPageQuery query) {
        domainConfigService.delete(query.getId());
        return QResult.success();
    }
}